<script setup>
import { useRouter, useRoute } from "vue-router";
import useDataStore from "@/store/index";
import { onUnmounted, ref, toRef, watch } from "vue";
import { storeToRefs } from "pinia";
import { searchAPI } from "@/apis/home";

const route = useRoute();
const router = useRouter();
const dataStore = useDataStore();

defineOptions({
  name: "headers",
});

const { userInfo, isLogin } = storeToRefs(dataStore);
// console.log(userInfo.value, isLogin.value);

let headersNavList = ref([
  {
    title: "笔记动态",
    name: "home",
    icon: "<EditPen />",
  },
  {
    title: "创作中心",
    name: "publish",
    icon: "<EditPen />",
  },
  {
    title: "聊天频道",
    name: "rate",
    icon: "<Star />",
  },
  // {
  //     title: '购物中心',
  //     name: 'shop',
  //     icon: '<Star />'
  // },
]);
let active = ref("");

// 让导航的高亮以当前路由为依据，而不是点击
const stopWatcher = watch(
  () => route.name,
  (newValue, oldValue) => {
    // console.log('change', newValue, oldValue);
    active.value = newValue;
  },
  {
    immediate: true,
  }
);

onUnmounted(() => {
  stopWatcher();
});

const btnAction = () => {
  if (isLogin.value) {
    router.push({ name: "user" });
    active.value = headersNavList.value.length + 1;
  } else {
    router.push({ name: "login" });
  }
};

/* -------------------------- 按需在不同的页面headers中隐藏搜索框 ------------------------- */
const props = defineProps({
  searchFlag: {
    default: false,
  },
});

const changeNav = (name, index) => {
  router.push({ name });
  active.value = name;
};

/* ------------------------------- 点击logo回到首页 ------------------------------- */
const toHomeAction = () => {
  router.push({ name: "home" });
  active.value = 0;
};

/* ---------------------------------- 搜索功能 ---------------------------------- */
let searchInput = ref(); //输入框内容
let drawer = ref(false); //是否显示drawer
let searchList = ref([]); // 查询到的搜索内容
// 获取焦点时监听键盘事件
const enterAction = () => {
  window.onkeydown = async (e) => {
    if (e.keyCode === 13) {
      drawer.value = true;
      let data = await searchAPI(searchInput.value);
      if (data.code === 200) {
        searchList.value = data.data;
      }
    }
  };
};
// 失去焦点时移除键盘事件
const cnacelEnterAction = () => {
  window.onkeydown = null;
};

/* --------------------------------- 点击进入详情页 -------------------------------- */
const toDetailAction = (article_id, article_author_id) => {
  router.push({
    name: "detail",
    query: { article_id, user_id: article_author_id },
  });
  drawer.value = false;
};
</script>

<template>
  <el-drawer
    v-model="drawer"
    size="90%"
    append-to-body="true"
    direction="ttb"
    title="I am the title"
    :with-header="false"
  >
    <div class="title">为您搜索到以下{{ searchList.length }}条结果:</div>
    <div
      @click="toDetailAction(item.article_id, item.article_author_id)"
      class="main-item"
      v-for="item in searchList"
      :key="item.article_id"
    >
      <div class="item-left">
        <div class="left-title">{{ item.article_title }}</div>
        <div class="left-introduction">{{ item.article_introduction }}</div>
        <!-- <div class="left-author">
                    <div>{{ item.user_name }}</div>
                    <div>
                        <el-icon>
                            <Pointer />
                        </el-icon>
                        {{ item.article_like }}
                    </div>
                </div> -->
      </div>
      <div class="item-right"><img :src="item.article_coverimg" alt="" /></div>
    </div>
  </el-drawer>
  <div class="headers-box">
    <div class="headers">
      <div class="headers-logo" @click="toHomeAction">
        <img src="../assets/images/shoesdogLogo1.png" alt="" />
      </div>
      <div class="headers-search">
        <el-input
          v-if="searchFlag"
          v-model="searchInput"
          size="large"
          autoSize
          placeholder="Welcome to shoesDog"
          suffix-icon="Search"
          @focus="enterAction"
          @blur="cnacelEnterAction"
        />
      </div>
      <div class="headers-nav">
        <div
          :class="{ navItem: true, navActive: active === item.name }"
          @click="changeNav(item.name, index)"
          v-for="(item, index) in headersNavList"
          :key="item.name"
        >
          <!-- <el-icon style="height: auto;" size="20">
                        <div v-html="item.icon"></div>
                    </el-icon> -->
          <div>{{ item.title }}</div>
        </div>
      </div>
      <div
        :class="{
          headerUser: true,
          userActive: active === headersNavList.length + 1,
        }"
      >
        <div @click="btnAction">
          {{ isLogin ? `欢迎 , ${userInfo.user_name}` : "请登录" }}
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.title {
  font-size: 20px;
  margin: 20px 0;
}

.main-item {
  cursor: pointer;
  font-family: "微软雅黑";
  width: 100%;
  height: 150px;
  box-sizing: border-box;
  box-shadow: #ddd 0px 0px 10px 3px;
  border-radius: 20px;
  margin: 15px 0;
  background-color: white;
  overflow: hidden;
  display: flex;
  padding: 10px 20px;
  justify-content: space-between;

  .item-left {
    width: 75%;
    text-align: left;

    .left-title {
      margin-top: 15px;
      font-size: 18px;
      font-weight: 600;
    }

    .left-introduction {
      font-size: 13px;
      line-height: 20px;
      margin-top: 20px;
      color: #aaa;
    }

    .left-author {
      font-size: 13px;
      width: 100%;
      margin-top: 20px;
      display: flex;
      align-items: center;

      > div:nth-child(1) {
        width: 80%;
      }
    }
  }

  .item-right {
    // width: 20%;
    // height: 100%;
    overflow: hidden;
    border-radius: 20px;

    img {
      width: 100%;
      height: 100%;
    }
  }
}

.headers-box {
  width: 100%;
  height: 60px;
  box-shadow: #eee 0 3px 5px 0;
  display: flex;
  justify-content: center;
  background-color: white;
  z-index: 1000;

  .headers {
    // width: 1300px;
    width: 90%;
    // height: 100%;
    display: flex;
    align-items: center;

    .headers-logo {
      margin-top: 5px;
      width: 200px;
      height: 100%;
      cursor: pointer;

      @media screen and (max-width: 600px) {
        width: 0;
      }

      img {
        width: 100%;
        height: 100%;
      }
    }

    .headers-search {
      width: 400px;
      margin-left: 40px;
      line-height: 60px;

      @media screen and (max-width: 600px) {
        margin-left: 0;
      }
    }

    .headers-nav {
      flex: 1;
      line-height: 60px;
      font-size: 15px;
      // margin-left: 100px;
      display: flex;
      justify-content: space-around;
      position: relative;

      @media screen and (max-width: 600px) {
        flex: none;
        width: 0;
      }

      .navItem {
        cursor: pointer;
        // width: 150px;
        margin: 0 15px;
        display: flex;
        transition: all 0.25s ease;
        text-align: center;

        div {
          width: 100%;
        }

        i {
          margin: 0 10px;
        }

        &:hover {
          color: #8e8cd8;
          font-weight: 600;
        }

        // &:nth-child(3):hover {
        // }

        // &:nth-child(2)::after {
        //     content: '';
        //     width: 2px;
        //     height: 40px;
        //     background-color: #eee;
        //     position: absolute;
        //     left: 352px;
        //     top: 20px;
        // }
      }

      .navActive {
        border-bottom: #8e8cd8 solid 1px;
        transition: all ease 0.5s;
      }
    }

    .headerUser {
      margin-left: 15px;
      width: 10%;
      position: relative;
      font-size: 15px;
      cursor: pointer;
      transition: all ease 0.5s;

      &::before {
        content: "";
        width: 2px;
        height: 40px;
        background-color: #eee;
        position: absolute;
        left: -30px;
        top: -10px;
      }

      &:hover {
        color: bisque;
      }
    }

    .userActive {
      color: bisque;
    }
  }
}
</style>
